package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.ITravelStatsCacheService;
import cn.wolfcode.luowowo.cache.service.IVerifyCodeCacheService;
import cn.wolfcode.luowowo.cache.vo.TravelStats;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.scenic.domain.ScenicComment;
import cn.wolfcode.luowowo.scenic.service.IScenicCommentService;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("mine")
public class MineController {

    @Reference
    private ITravelService travelService;
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelStatsCacheService travelStatsCacheService;
    @Reference
    private IVerifyCodeCacheService verifyCodeCacheService;

    @Reference
    private IScenicCommentService scenicCommentService;

    @RequestMapping("/mytravelnotes")
    @RequiredLogin
    public String mytravelnotes(@ModelAttribute("qo") TravelQueryObject qo, Model model, @LoginUser UserInfo user) {
        // /获取游记
        qo.setUserId(user.getId());
        PageInfo<Travel> userIdTrave = travelService.getUserIdTrave(qo);
        List<Travel> list = userIdTrave.getList();

        //该作者游记总访问量
        int allView = 0;
        //该作者游记今日访问量
        int todayView = 0;
        for (Travel t : list) {

            //设置当日阅读数
            TravelStats travelStats = travelStatsCacheService.getTravelStats(t.getId());
            t.setTodayviewnum(travelStats.getTodayviewnum());
            //去热数据里获取设置总阅读数
            t.setViewnum(travelStats.getViewnum());

            //获得游记总访问量
            allView += t.getViewnum();
            //获取游记今日访问量
            todayView += t.getTodayviewnum();

            //获取国家id
            Destination province = destinationService.getCountry(t.getDest().getId());
            //设置国家
            if (!t.getDest().getName().equals(province.getName())) {
                t.getDest().setParent(province);
                TravelContent content = travelService.getContent(t.getId());
                t.setTravelContent(content);
            }
        }

        //获取这个用户的所有游记的阅读数
        Integer totalViewnum = travelService.getUserViewnum(user.getId());

        //获取这个用户的所有游记的评论数
        Integer totolReplynum = travelService.getUserReplynum(user.getId());

        model.addAttribute("allView", allView);
        model.addAttribute("todayView", todayView);
        model.addAttribute("u", user);
        model.addAttribute("totalViewnum", totalViewnum);
        model.addAttribute("totolReplynum", totolReplynum);
        model.addAttribute("pageInfo", userIdTrave);
        model.addAttribute("user", user);
        return "mine/mytravelnotes";
    }

    @RequiredLogin
    @RequestMapping("home")
    public String home(Model model, @LoginUser UserInfo user, TravelQueryObject qo) {
        PageInfo<Travel> pageInfo = travelService.query(qo);
        //获取文章
        for (Travel t : pageInfo.getList()) {

            //设置当日阅读数
            TravelStats travelStats = travelStatsCacheService.getTravelStats(t.getId());
            t.setTodayviewnum(travelStats.getTodayviewnum());
            //去热数据里获取设置总阅读数
            t.setViewnum(travelStats.getViewnum());

            //获取国家id
            Destination province = destinationService.getCountry(t.getDest().getId());
            //设置国家
            if (!t.getDest().getName().equals(province.getName())) {
                t.getDest().setParent(province);
                TravelContent content = travelService.getContent(t.getId());
                t.setTravelContent(content);
            }
        }

        model.addAttribute("tsize", travelService.list().size());
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("user", user);
        return "mine/homepage";
    }

    @RequiredLogin
    @RequestMapping("review")
    public String review(Model model, @LoginUser UserInfo user, Long type) {
        List<ScenicComment> reviewList = scenicCommentService.getByUserId(user.getId(), type);
        int totalReview = scenicCommentService.getTotalReview(user.getId(), 0L);
        int totalGoldReview = scenicCommentService.getTotalReview(user.getId(), 1L);
        //防止评论有多张图导致网页不识别,获取图片数据,切割并重新赋值
//        for (ScenicComment s : reviewList) {
//            String[] img = s.getImgUrls().toString().split(",");
//            s.setImgUrls(img[0]);
//        }
        //通过用户id获取用户名下所有文章总点赞数的方法
        Long sum = travelService.selectThumbsupnumTotal(user.getId());
        model.addAttribute("thumbsum", sum);
        model.addAttribute("user", userInfoService.getUser(user.getId()));
        model.addAttribute("reviewList", reviewList);
        model.addAttribute("totalReview", totalReview);
        model.addAttribute("totalGoldReview", totalGoldReview);
        return "mine/review";
    }

    @RequiredLogin
    @RequestMapping("travelcollection")
    public String travelcollection(Model model, @LoginUser UserInfo user, TravelQueryObject qo) {
        model.addAttribute("user", user);

        qo.setUserId(user.getId());
        PageInfo<Travel> travelPageInfo = travelService.listByUserFavor(qo);
        PageInfo<StrategyDetail> strategyDetailPageInfo = strategyDetailService.listByUserFavor(qo);
        PageInfo<Destination> destinationPageInfo = destinationService.listByUserFavor(qo);


        //收藏的地点
        model.addAttribute("destPageInfo", destinationPageInfo);
        //收藏的游记列表
        model.addAttribute("travelPageInfo", travelPageInfo);
        //收藏的攻略列列表
        model.addAttribute("strategyDetailPageInfo", strategyDetailPageInfo);

        return "mine/travelcollection";
    }

    @RequiredLogin
    @RequestMapping("setting")
    public String setting(Model model, @LoginUser UserInfo user) {
        String phone = user.getPhone();
        user = userInfoService.getUser(user.getId());
        user.setPhone(phone.substring(0, 3) + "****" + phone.substring(7));
        model.addAttribute("user", user);
        return "mine/setting";
    }

    @RequiredLogin
    @RequestMapping("InfoUpdate")
    public String InfoUpdate(@ModelAttribute("user") UserInfo user) {
        userInfoService.update(user);
        return "redirect:/mine/setting";
    }

    @RequiredLogin
    @RequestMapping("deleteIsFavor")
    @ResponseBody
    public Object deleteIsFavor(@LoginUser UserInfo user, Long travelId) {
        AjaxResult result = new AjaxResult();
        travelService.deleteIsFavor(user, travelId);
        return result;
    }

    @RequiredLogin
    @RequestMapping("byphone")
    @ResponseBody
    public Object byphone(@LoginUser UserInfo user,String code,String password,String rpassword) {
        AjaxResult result = new AjaxResult();
        try {
            String verifyCode = verifyCodeCacheService.getVerifyCode(user.getPhone());

            if(!code.equals(verifyCode)){
                result.setSuccess(false);
                result.mark("验证码错误");
            }
            user.setPassword(password);
            userInfoService.updateUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
